package com.pethome.web.controller;

import com.pethome.common.annotation.Log;
import com.pethome.common.core.controller.BaseController;
import com.pethome.common.core.domain.AjaxResult;
import com.pethome.common.core.page.TableDataInfo;
import com.pethome.common.enums.BusinessType;
import com.pethome.common.utils.poi.ExcelUtil;
import com.pethome.web.domain.Post;
import com.pethome.web.domain.vo.PostAndCustomer;
import com.pethome.web.service.IPostService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 帖子管理Controller
 * 
 * @author 胡继承
 * @date 2024-09-04
 */
@RestController
@RequestMapping("/web/post")
@Slf4j
public class PostController extends BaseController
{
    @Autowired
    private IPostService postService;

    /**   todo  所有数据全部存到本地在本地查询 没有看数据库查和本地查哪个快
     * 顾客获取全部帖子信息
     * @return 全部帖子信息
     */
    @GetMapping("/getPostList")
    public TableDataInfo getPostList()
    {
        List<PostAndCustomer> list = postService.selectPostAndCustomerList();

        return getDataTable(list);
    }

    //顾客发表帖子
    @PostMapping("/addPost")
    public AjaxResult addPost(@RequestBody Post post)
    {
        return toAjax(postService.insertPost(post));
    }

    //通过id获取帖子信息
    @GetMapping("/getPostById/{id}")
    public AjaxResult getPostById(@PathVariable("id") Integer postId){
        log.warn(postId.toString());
        return success(postService.selectPostAllInfoByPostId(postId));
        }
    //搜索文章
    @GetMapping("/searchPost")
    public TableDataInfo searchPost(@RequestParam("postTitle") String postTitle)
    {
        //log.warn(postTitle+"|=================");
        //防止输入的内容为空
        if(StringUtils.isBlank(postTitle)){
            postTitle = null;
        };
        List<PostAndCustomer> list = postService.searchPost(postTitle);
        return getDataTable(list);
    }

    @GetMapping("/addReadCount")
     public AjaxResult addReadCount(@RequestParam("postId") Integer postId){
        //todo   幻读 脏写
        return postService.addReadCounnt(postId);
    }
    //根据作者id获取相关文章信息
    @GetMapping("/searchPostByCustomerId")
    public TableDataInfo searchPostByCustomerId(@RequestParam("customerId") Integer customerId){
        List<Post> list = postService.searchPostByCustomerId(customerId);
        return getDataTable(list);
    }


//==========================================================================

    /**
     * 查询帖子管理列表
     */
    @PreAuthorize("@ss.hasPermi('web:post:list')")
    @GetMapping("/list")
    public TableDataInfo list(Post post)
    {
        startPage();
        List<Post> list = postService.selectPostList(post);
        return getDataTable(list);
    }

    /**
     * 导出帖子管理列表
     */
    @PreAuthorize("@ss.hasPermi('web:post:export')")
    @Log(title = "帖子管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Post post)
    {
        List<Post> list = postService.selectPostList(post);
        ExcelUtil<Post> util = new ExcelUtil<Post>(Post.class);
        util.exportExcel(response, list, "帖子管理数据");
    }

    /**
     * 获取帖子管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('web:post:query')")
    @GetMapping(value = "/{postId}")
    public AjaxResult getInfo(@PathVariable("postId") Long postId)
    {
        return success(postService.selectPostByPostId(postId));
    }

    /**
     * 新增帖子管理
     */
    @PreAuthorize("@ss.hasPermi('web:post:add')")
    @Log(title = "帖子管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Post post)
    {
        return toAjax(postService.insertPost(post));
    }

    /**
     * 修改帖子管理
     */
    @PreAuthorize("@ss.hasPermi('web:post:edit')")
    @Log(title = "帖子管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Post post)
    {
        return toAjax(postService.updatePost(post));
    }

    /**
     * 删除帖子管理
     */
    @PreAuthorize("@ss.hasPermi('web:post:remove')")
    @Log(title = "帖子管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{postIds}")
    public AjaxResult remove(@PathVariable Long[] postIds)
    {
        return toAjax(postService.deletePostByPostIds(postIds));
    }
}
